package com.mijie.homi.service.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.homi.common.domain.Grade;
import com.mijie.homi.common.domain.Integration;
import com.mijie.homi.common.domain.Upgrade;
import com.mijie.homi.dao.commom.IntegrationDao;
import com.mijie.homi.dao.commom.MixDao;
import com.mijie.homi.domain.commom.EmailVerify;
import com.mijie.homi.domain.commom.ResetPassword;
import com.mijie.util.Helper;

@Service
public class MixService {

	@Autowired
	private IntegrationDao integrationDao;
	@Autowired
	private MixDao mixDao;
	
	
	//=========================email verify ================================
	
	public void emailVerifyApply(int userId,String email,String code){
		mixDao.emailVerifyApply(userId, email, code);
	}
	
	public EmailVerify getEmailVerifyApply(String code){
		return mixDao.getEmailVerifyApply(code);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public void deleteEmailVerifyApply(String code){
		mixDao.deleteEmailVerifyApply(code);
	}
	
	//=========================password rest ================================
	public void resetPasswordApply(int userId,String code){
		mixDao.resetPasswordApply(userId, code);
	}
	
	public ResetPassword getResetPasswordApply(String code){
		return mixDao.getResetPasswordApply(code);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public void deleteResetPasswordApply(String code){
		mixDao.deleteResetPasswordApply(code);
	}
	
	public Grade getRefGradeByIntegral(int integral){
		return mixDao.getRefGradeByIntegral(integral);
	}
	
	public Integration getIntegrationById(int id){
		return integrationDao.getIntegrationById(id);
	}
	
	public Integer getTodayIntegralReward(int userId,int actionId){
		return integrationDao.getTodayIntegralReward(userId, actionId);
	}
	
	public boolean haveReward(int id,int actionId){
		return integrationDao.haveReward(id,actionId);
	}
	
	public int updateIntegralRewardDetail(int userId,int actionId,int integral){
		return integrationDao.updateIntegralRewardDetail(userId, actionId, integral);
	}
	
	public Upgrade getIphoneUpgrade(String versionName){
		Upgrade upgrade =  mixDao.getIphoneUpgrade();
		if(upgrade!=null){
			if(!Helper.checkUpgrade(versionName, upgrade.getVersionName())) upgrade = null;
		}
		return upgrade;
	}
	
}
